package com.jmx.yp.service.member;

import java.util.*;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import com.jmx.yp.dao.IBaseDao;
import com.jmx.yp.entity.AreaInfo;

/**
 * 
 * 功能描述：
 *			接口IMemberService实现类
 * @author Jenny
 *
 * Date:2011-2-18 下午03:40:41
 */
public class MemberService implements IMemberService {
	private IBaseDao dao;
	
	private final String ROOT_IM0 = "folderClosed.gif";
	
	private final String ROOT_IM1 ="folderOpen.gif";
	
	private final String ROOT_IM2 = "folderClosed.gif";
		
	private final String CHILD_IM0 = "book_titel.gif";
	
	private final String CHILD_IM1 = "book.gif";
	
	private final String CHILD_IM2 = "book_titel.gif";
	
	public void setDao(IBaseDao dao){
		this.dao = dao;
	}

	/**
	 * 根据组织ID获取所属区域信息
	 * @param groupId
	 * @return 包仿Map<AREA_NAME, SERIALID>对象的LIST 
	 * @throws Exception
	 */
	public List getAreaByGroupId(Long groupId) throws Exception{
		if(groupId == null)
			return null;
		List list = dao.getList("com.jmx.yp.entity.MemberMapper.getArea", groupId);
		return list;
	}
	
	/**
	 * 根据节点ID查询区域 
	 * @param areaId areaId及area_name都为空时，表示查询最顶层的结点（即uplevel=0)
	 * 				 若areaId不为空，area_name为空，则表示节点展开查询
	 * 				 若areaId为空，area_name不为空，则表示按照名称查询树及其子树
	 * @return
	 * @throws Exception
	 */
	public Document getAreaList(String area_name, Long areaId) throws Exception{
		List areaList = null;
		AreaInfo area = null;
		if(area_name == null || area_name.length() <= 0){
			if(areaId == null)
				areaId = 0l;  //赋值为0，进行查询
			areaList = dao.getList("com.jmx.yp.entity.MemberMapper.getAreaChild", areaId);
			
		}else{
			area = (AreaInfo)dao.getObject("com.jmx.yp.entity.MemberMapper.getAreaObject", area_name);
			if(area.getIsleaf() == AreaInfo.LEAF_YES){
				areaList = dao.getList("com.jmx.yp.entity.MemberMapper.getAreaChild", area.getSerialid());
			}
		}
		
		Document doc = buildXMLDocument(area, areaList);
		return doc;
	}
	
	/**
	 * 构造DOCUMENT
	 * @param area
	 * @param list
	 * @return
	 */
	private Document buildXMLDocument(AreaInfo area, List list){
		 Document document = DocumentHelper.createDocument();   
	     document.setXMLEncoding("utf-8");
	     
		if(area == null && (list == null || list.isEmpty()))
			return document;
		
		Element tree = document.addElement("tree");   
	    tree.addAttribute("id", "0");
		
		if(area != null){
			 //加入显示树根结点	
		     Element rootItem = tree.addElement("item");
		     rootItem.addAttribute("text", area.getArea_name());
		     rootItem.addAttribute("id", area.getSerialid() + ";" + area.getArea_code() + ";" + 
		    		 (area.getIsleaf().intValue() == AreaInfo.LEAF_YES?"true":"false"));
		     if(area.getIsleaf() == AreaInfo.LEAF_YES){
		    	 rootItem.addAttribute("open", "1");
		    	 rootItem.addAttribute("im0", ROOT_IM0);
		    	 rootItem.addAttribute("im1", ROOT_IM1);
		    	 rootItem.addAttribute("im2", ROOT_IM2);
		     }else{
		    	 rootItem.addAttribute("im0", CHILD_IM0);   
		    	 rootItem.addAttribute("im1", CHILD_IM1);   
		    	 rootItem.addAttribute("im2", CHILD_IM2);
		     }
		     
		     if(area.getIsleaf() == AreaInfo.LEAF_YES){
		    	 for(Iterator<?> its = list.iterator(); its.hasNext();){
		    		 AreaInfo a = (AreaInfo)its.next();
		    		 getSubElements(rootItem, a);
		    	 }
		     }
		}else{
			 for(Iterator<?> its = list.iterator(); its.hasNext();){
	    		 AreaInfo a = (AreaInfo)its.next();
	    		 getSubElements(tree, a);
	    	 }
		}
		
		return document;
	}
	
	/**
	 * 构造子元素
	 * @param element
	 * @param group
	 */
	private Element getSubElements(Element element, AreaInfo area){
		 Element subItem = element.addElement("item");
		 subItem.addAttribute("text", area.getArea_name());   
	     subItem.addAttribute("id", area.getSerialid() + ";" + area.getArea_code() + ";" +  
	    		 (area.getIsleaf().intValue() == AreaInfo.LEAF_YES?"true":"false"));
	     
	     if(area.getIsleaf() == AreaInfo.LEAF_YES){	    	
	    	 subItem.addAttribute("im0", ROOT_IM0);   
	    	 subItem.addAttribute("im1", ROOT_IM1);   
	    	 subItem.addAttribute("im2", ROOT_IM2);
	    	 subItem.addElement("item").addAttribute("text", "haha");
	     }else{
	    	 subItem.addAttribute("im0", CHILD_IM0);   
	    	 subItem.addAttribute("im1", CHILD_IM1);   
	    	 subItem.addAttribute("im2", CHILD_IM2);
	     }
	     return subItem;
	}
	
	/**
	 * 根据节点ID查询子区域 
	 * @param areaId
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public Map getSubAreaList(Long areaId) throws Exception{
		List list = dao.getList("com.jmx.yp.entity.MemberMapper.getAreaChild", areaId);
		Map map = new HashMap(1);
		map.put("rows", rebuildArea(list));
		return map;
	}
	
	/**
	 * 重组Map
	 * @param list
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private List<Map> rebuildArea(List list){
		if(list == null || list.isEmpty())
			return null;
		List<Map> tList = new ArrayList<Map>(list.size());
		for(Iterator its = list.iterator(); its.hasNext();){
			AreaInfo area = (AreaInfo)its.next();
			Map map = new HashMap();
			
			map.put("text", area.getArea_name());   
		    map.put("id", area.getSerialid() + ";" + area.getArea_code() + ";" +  
		    		 (area.getIsleaf().intValue() == AreaInfo.LEAF_YES?"true":"false"));
		     
		    if(area.getIsleaf() == AreaInfo.LEAF_YES){	    	
		    	 map.put("im0", ROOT_IM0);   
		    	 map.put("im1", ROOT_IM1);   
		    	 map.put("im2", ROOT_IM2);
		    	 map.put("child", true);
		    }else{
		    	 map.put("im0", CHILD_IM0);   
		    	 map.put("im1", CHILD_IM1);   
		    	 map.put("im2", CHILD_IM2);
		    	 map.put("child", false);
		    }
		    tList.add(map);
		}
		return tList;
	}
}
